import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/index'
import store from '../store/index'
import {
  checkusertokenid
} from '../api/user'
import {
  Toast
} from 'vant';
/**
 * requireAuth：是否需要登录
 */

Vue.use(VueRouter)
let router = new VueRouter({
  routes: [{
      path: '/',
      redirect: '/centerzh'
    }, {
      path: '/centerzh',
      name: 'centerzh',
      component: () => import('../views/authPage/centerzh.vue'),
      meta: {
        title: '首页',
        path: 'zh',
        keepALive: true,
        active: 0,
        mennuactive: 0,
        nofooter: true,
        loginPage: true
      }
    },
    {
      path: '/centertcl',
      name: 'centertcl',
      component: () => import('../views/authPage/centertcl.vue'),
      meta: {
        title: '炭材料',
        keepALive: true,
        active: 0,
        mennuactive: 1,
        path: 'tcl',
        nofooter: true,
        // loginPage: true
      }
    }, {
      path: '/centerdc',
      name: 'centerdc',
      component: () => import('../views/authPage/centerdc.vue'),
      meta: {
        title: '电池',
        active: 0,
        mennuactive: 2,
        path: 'dc',
        keepALive: true,

        nofooter: true,
        // loginPage: true
      }
    }, {
      path: '/centerdlg',
      name: 'centerdlg',
      component: () => import('../views/authPage/centerdlg.vue'),
      meta: {
        title: '电炉钢',
        active: 0,
        mennuactive: 3,
        path: "dlg",
        keepALive: true,
        nofooter: true,
        // loginPage: true
      }
    },
    {
      path: '/person',
      name: 'person',
      component: () => import('../views/authPage/person.vue'),
      meta: {
        title: '我的中心',
        active: 3,
        nofooter: true,
        loginPage: true
      }
    }, {
      path: '/report',
      name: 'report',
      component: () => import('../views/authPage/report.vue'),
      meta: {
        title: '报告',
        active: 1,
        nav: true,
        nofooter: true,
        keepALive: true,
      }
    }, {
      path: '/meeting',
      name: 'meeting',
      component: () => import('../views/authPage/meeting.vue'),
      meta: {
        title: '会议',
        nav: true,
        active: 2,
        nofooter: true,
        keepALive: true
      }
    },
    {
      path: '/meetDetails',
      name: 'meetDetails',
      component: () => import('../views/part/meetDetails.vue'),
      meta: {
        title: '会议详情',
        nav: true,

      }
    },

    {
      path: '/newsDetails',
      name: 'newsDetails',
      component: () => import('../views/part/newsDetails.vue'),
      meta: {
        title: '',
        nofooter: true,
      }
    },
    {
      path: '/newsshare',
      name: 'newsshare',
      component: () => import('../views/part/newsDetailshare.vue'),
      meta: {
        title: '',
      }
    },
    // {
    //   path: '/centerchannelnews',
    //   name: 'centerchannelnews',
    //   component: () => import('../views/part/centerchannelnews.vue'),
    //   meta: {
    //     title: '搜索',
    //     active: 0,
    //     nofooter:true,
    //     keepALive:true,
    //     nav:true,
    //   }
    // },
    {
      path: '/priceparttcl',
      name: 'priceparttcl',
      component: () => import('../views/part/priceparttcl.vue'),
      meta: {
        keepALive:true
      },
      redirect:"/tcl",
      children:[
        {
          path: '/tcl',
          name: 'tcl',
          component: () => import('../views/part/pricepart.vue'),
          meta: {
            keepALive:true
          },
        }
      ]
    },
    {
      path: '/pricepartdc',
          name: 'pricepartdc',
      component: () => import('../views/part/pricepartdc.vue'),
      meta: {
        keepALive:true
      },
      redirect:"/dc",
      children:[
        {
          path: '/dc',
          name: 'dc',
          component: () => import('../views/part/pricepart.vue'),
          meta: {
            keepALive:true
          },
        }
      ]
    },
    {
      path: '/pricepartdlg',
      name: 'pricepartdlg',
      component: () => import('../views/part/pricepartdlg.vue'),
      meta: {
        keepALive:true
      },
      redirect:"/dlg",
      children:[
        {
          path: '/dlg',
          name: 'dlg',
          component: () => import('../views/part/pricepart.vue'),
          meta: {
            keepALive:true
          },
        }
      ]
    },
   
    {
      path: '/priceDetail',
      name: 'priceDetail',
      component: () => import('../views/part/priceDetail.vue'),
      meta: {
        title: '价格-详情',
        nofooter: false,
      }
    },
    {
      path: '/priceQs',
      name: 'priceQs',
      component: () => import('../views/part/priceQs.vue'),
      meta: {
        title: '价格-趋势',
        // nofooter: true,
      }
    },
    {
      path: '/priceSj',
      name: 'priceSj',
      component: () => import('../views/part/priceSj.vue'),
      meta: {
        title: '价格-数据',
        nofooter: true,
        nav: true
      }
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('../views/part/serchPage.vue'),
      meta: {
        title: '',
        keepALive: true,
        // nofooter: true,
      }
    },
    {
      path: '/consultpart',
      name: 'consultpart',
      component: () => import('../views/part/consultpart.vue'),
      meta: {
        title: '资讯',
        nofooter: true,
        nav: true,

      }
    },

    {
      path: '/datumpart',
      name: 'datumpart',
      component: () => import('../views/part/datumpart.vue'),
      meta: {
        title: '数据',
        // nofooter: true,
        nav: true,
        loginPage: true
      }
    },
    {
      path: '/datuDetail',
      name: 'datuDetail',
      component: () => import('../views/part/datuDetail.vue'),
      meta: {
        title: '数据详情',
        nofooter: true,
      }
    },
    {
      path: '/reportpart',
      name: 'reportpart',
      component: () => import('../views/part/reportpart.vue'),
      meta: {
        title: '报告',
        nofooter: true,
        nav: true,

      }
    },
    {
      path: '/replaypart',
      name: 'replaypart',
      component: () => import('../views/part/replaypart.vue'),
      meta: {
        title: '供求',
        nofooter: true,

      }
    },
    {
      path: '/iconfive',
      name: 'iconfive',
      component: () => import('../views/part/iconfive.vue'),
      meta: {
        title: "",
        nofooter: true,
        nav: true,
        nofooter: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/loginAndregister/login.vue'),
      meta: {
        title: '登录',
        nofooter: false,
        nav: true,
      }
    },
    {
      path: '/fbgq',
      name: 'fbgq',
      component: () => import('../views/part/fbgq.vue'),
      meta: {
        title: '发布供求',
        nofooter: false,
        nav: true
      }
    },
    {
      path: '/qgList',
      name: 'qgList',
      component: () => import('../views/part/gqList.vue'),
      meta: {
        title: '我的求购',
        nofooter: false,
        nav: true
      }
    },
    {
      path: '/gyList',
      name: 'gyList',
      component: () => import('../views/part/gqList.vue'),
      meta: {
        title: '我的供应',
        nofooter: false,
        nav: true
      }
    },

    {
      path: '/changePsd',
      name: 'changePsd',
      component: () => import('../views/loginAndregister/changePsd.vue'),
      meta: {
        title: '修改密码',
        nofooter: false,
        nav: true
      }
    },
    {
      path: '/start',
      name: 'start',
      component: () => import('../views/loginAndregister/start.vue'),
      meta: {
        title: '启动页',
        nofooter: false
      }
    }, {
      path: '/register',
      name: 'register',
      component: () => import('../views/loginAndregister/register.vue'),
      meta: {
        title: '自助注册',
        nofooter: false,
        nav: true
      }
    },
    {
      path: '/registershare',
      name: 'registershare',
      component: () => import('../views/loginAndregister/registershare.vue'),
      meta: {
        title: '自助注册',
        nofooter: false,
        nav: true
      }
    }
  ],
  scrollBehavior(to, from, savedPosition) {
    return {
      x: 0,
      y: 0
    }
  }
})

router.beforeEach((to, from, next) => {
 
  Toast.clear();
  if (to.name == 'start') {
    next();
  } else if(to.name==="newsshare" || to.name==="registershare" ){
    next();
  } else if (to.name !== 'start' && store.state.startPage) {
    //新闻详情返回后，组件是保存的
    if (from.name == 'newsDetails' && to.name != 'newsDetails') {
      to.meta.keepALive = true;
    }
    if (to.meta.loginPage) {
      //进入前需要判断是否要登录的接口
      let obj = {}
      obj.channel = to.query.channel || 'all'
      checkusertokenid(obj).then(res => {
        if (res.code == '0000') {
          store.dispatch('acislogin', true)
        } else {
          store.dispatch('acislogin', false)
        }
        next();
      })
    } else {
      //登录注册页面如果判断有登陆要直接进入首页
      if (to.name == 'login' || to.name == 'register') {
        let obj = {}
        obj.channel = to.query.channel || 'all'
        checkusertokenid(obj).then(res => {
          if (res.code == '0000') {
            store.dispatch('acislogin', true)
            next({
              path: '/centerzh'
            });
          } else {
            store.dispatch('acislogin', false)
            next();
          }
        })
      } else {
        next();
      }
    }
  } else {
    next({
      path: 'start'
    });
  }
})
router.afterEach( (to, from, next)=>{
  let auth=(to.name=="dc" || to.name=="dlg" || to.name=="tcl");
  if (from.name=="priceDetail" && auth) {
     to.meta.back=true
  }else if (auth) {
    to.meta.back=false
  } 
  
}
)

export default router
